/* ************************************************************************
> File Name:     qsort.c
> Author:        zhfu
> mail:          3346897597@qq.com
> Created Time:  2022年04月27日 星期三 11时15分29秒
> Description:   
 ************************************************************************/
#include"qsort.h"

void swap(void *data[], int a1, int a2)
{
    void *tmp;
    tmp = data[a1];
    data[a1] = data[a2];
    data[a2] = tmp;
}

void qsort(cmp_t cmp, int left, int right, void *data[])
{
    if (left >= right){
        return;
    }
    
    swap(data, left, (left + right) / 2);
    int last = left;

    int i;
    for (i = left + 1; i <= right; ++i){
        if (cmp(data[i], data[left]) < 0){
            swap(data, i, ++last);
        }
    }

    swap(data, left, last);
    qsort(cmp, left, last - 1, data);
    qsort(cmp, last + 1, right, data);
}
